package jgrit.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import jgrit.gui.factory.UIFactory;
import jgrit.options.OptionCollection;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MetaOptionsPanel extends javax.swing.JPanel {

	/**
	 * Auto-generated serialization version ID
	 */
	private static final long serialVersionUID = 2017455843876404150L;

	private JComboBox metaTileSizeBox;
	private JTextField metaTileY;
	private JTextField metaTileX;
	
	/**
	 * "custom" option in the combo box
	 */
	private static final String CUSTOM = "custom"; 

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new MetaOptionsPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	@Deprecated
	public MetaOptionsPanel() {
		this(new OptionCollection());
	}
	
	public MetaOptionsPanel(OptionCollection options) {
		super();
		initGUI(options);
	}
	
	private void initGUI(OptionCollection options) {
		try {
			FormLayout thisLayout = new FormLayout(
					"max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;20dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;20dlu), 5dlu", 
					"max(p;5dlu), max(p;5dlu), max(p;5dlu)");
			// Lock the corresponding columns to the same width
			thisLayout.setColumnGroups(new int[][]{ {7, 11}, {9, 13} });
			this.setLayout(thisLayout);
			{
				ComboBoxModel metaTileSizeBoxModel = 
					new DefaultComboBoxModel(
							new String[] {
									"1x1", "2x2", "4x4", "8x8",
									"1x2", "1x4", "2x4", "4x8",
									"2x1", "4x1", "4x2", "8x4",
									CUSTOM
									});
				metaTileSizeBox = new JComboBox();
				this.add(metaTileSizeBox, new CellConstraints("4, 2, 1, 1, default, default"));
				metaTileSizeBox.setModel(metaTileSizeBoxModel);
				metaTileSizeBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						metaTileSizeBoxActionPerformed(evt);
					}
				});
			}
			{
				final JLabel jLabel1 = new JLabel();
				this.add(jLabel1, new CellConstraints("2, 2, 1, 1, default, default"));
				jLabel1.setText("Metatile size");
			}
			{
				final JLabel jLabel2 = new JLabel();
				this.add(jLabel2, new CellConstraints("7, 2, 1, 1, default, default"));
				jLabel2.setText("x:");
			}
			{
				metaTileX = new JTextField();
				this.add(metaTileX, new CellConstraints("9, 2, 1, 1, default, default"));
//				metaTileX.setText("1");
				UIFactory.attachOptionChecked(options.metaTileWidth, metaTileX);
				metaTileX.addKeyListener(new KeyAdapter() {
					public void keyTyped(KeyEvent e) {
						intFieldChanged();
					}
				});
			}
			{
				final JLabel jLabel3 = new JLabel();
				this.add(jLabel3, new CellConstraints("11, 2, 1, 1, default, default"));
				jLabel3.setText("y:");
			}
			{
				metaTileY = new JTextField();
				this.add(metaTileY, new CellConstraints("13, 2, 1, 1, default, default"));
//				metaTileY.setText("1");
				UIFactory.attachOptionChecked(options.metaTileHeight, metaTileY);
				
				metaTileY.addKeyListener(new KeyAdapter() {
					public void keyTyped(KeyEvent e) {
						intFieldChanged();
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * When the combo box selection changes, update the corresponding text field
	 * 
	 * @param evt Information about the event that fired
	 */
	private void metaTileSizeBoxActionPerformed(ActionEvent evt) {
		if (!metaTileSizeBox.getSelectedItem().toString().equals(CUSTOM)) {
			String[] selected = metaTileSizeBox.getSelectedItem().toString()
					.split("x", 2);
			metaTileX.setText(selected[0]);
			metaTileY.setText(selected[1]);
		}
	}
	
	/**
	 * When the text boxes are changed by the user, change the combo box to
	 * the corresponding setting or to "Custom".
	 * 
	 * For now, just set to custom.
	 */
	private void intFieldChanged() {
//		final String newValue = metaTileX.getText() + "x" + metaTileY.getText();
//		
//		final Runnable changeSizeBox = new Runnable() {
//			public void run() {
//				metaTileSizeBox.setSelectedItem(CUSTOM);
//				metaTileSizeBox.setSelectedItem(newValue);
//			}
//		};
//		
//		if (!metaTileSizeBox.getSelectedItem().equals(newValue)) {
//			SwingUtilities.invokeLater(changeSizeBox);
//		}
		
		metaTileSizeBox.setSelectedItem(CUSTOM);
	}
}
